package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /**
     * 新增敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult addSensitiveWord(AdSensitive adSensitive) {
        //1，参数校验
        if (StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2，检查是否重复
        List<AdSensitive> list = this.list(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (list.size() > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //3，插入数据
        adSensitive.setCreatedTime(new Date());
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByPage(SensitiveDto dto) {
        //1，参数校验
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2,查询数据
        IPage iPage = new Page(dto.getPage(),dto.getSize());
        if(StringUtils.isNoneBlank(dto.getName())){
            LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
            wrapper.like(AdSensitive::getSensitives,dto.getName());
            this.page(iPage,wrapper);
        }else {
            this.page(iPage);
        }

        //3. 封装结果
        return new ResponseResult(200,"查询成功",iPage.getRecords());
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update2(AdSensitive adSensitive) {
        //1，校验参数
        if(StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2，检查是否重复
        List<AdSensitive> list = this.list(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives,adSensitive.getSensitives()));
        if (list.size()>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已经存在");
        }
        //3,修改
        this.updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete2(Integer id) {
        //1，校验数据
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2,校验数据是否存在
        AdSensitive adSensitive = this.getById(id);
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3,删除数据
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
